
package andre.grids.filesystem.client;

import andre.grids.filesystem.common.DirectoryOperations;
import andre.grids.filesystem.common.FileOperations;

import java.rmi.*;
import java.rmi.registry.*;

/**
 *
 * @author andre
 */
public class RemoteServer {
    
    private String hostName;
    private int portNumber;
    private DirectoryOperations dirOp;
    private FileOperations fileOp;
    
    public RemoteServer(String name, int port, String fileOpBindName, String dirOpBindName) throws RemoteException, NotBoundException {
        hostName = name;
        portNumber = port;
        initObjects(fileOpBindName,dirOpBindName);
    }
    
    public RemoteServer(String serverName) throws RemoteException, NotBoundException {
        this(serverName,10101,"FileOperations.rem","DirectoryOperations.rem");
    }
    
    public String getHostName() {
        return this.hostName;
    }
    
    private void initObjects(String fileBind, String dirBind) throws RemoteException, NotBoundException {
        Registry reg = LocateRegistry.getRegistry(hostName, portNumber);
        fileOp = (FileOperations)reg.lookup(fileBind);
        dirOp = (DirectoryOperations)reg.lookup(dirBind);
    }
    
    public FileOperations getFileOp() {
        return fileOp;
    }
    
    public DirectoryOperations getDirOp() {
        return dirOp;
    }
    
    public static RemoteServer getServer(String hostName, int port, String fileBind, String dirBind) throws Exception {
        return new RemoteServer(hostName, port, fileBind, dirBind);
    }
    
    private static String defaultServer;
    private static int defaultPort;
    private static String defaultFileBind;
    private static String defaultDirBind;

    public static String getDefaultDirBind() {
        return defaultDirBind;
    }

    public static void setDefaultDirBind(String defaultDirBind) {
        RemoteServer.defaultDirBind = defaultDirBind;
    }

    public static String getDefaultFileBind() {
        return defaultFileBind;
    }

    public static void setDefaultFileBind(String defaultFileBind) {
        RemoteServer.defaultFileBind = defaultFileBind;
    }

    public static int getDefaultPort() {
        return defaultPort;
    }

    public static void setDefaultPort(int defaultPort) {
        RemoteServer.defaultPort = defaultPort;
    }

    public static String getDefaultServer() {
        return defaultServer;
    }

    public static void setDefaultServer(String defaultServer) {
        RemoteServer.defaultServer = defaultServer;
    }
}
